import time
from selenium.webdriver import ActionChains,Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class Base():
    def __init__(self,driver):
        self.driver=driver
    def get(self,url):
        self.driver.get(url)
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver, 10, 0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as f:
            print(f)
    # 截图
    def jietu(self):
        t=time.strftime('%Y%m%d-%H%M%S',time.localtime())
        self.driver.save_screenshot(f'{t}.png')
    def click(self,loc):
        self.find_element(loc).click()
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    # 断言
    def text(self,loc):
        return self.find_element(loc).text
    def jinframe(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(loc))
    def tuiframe(self):
        self.driver.switch_to.default_content()
    def qiehuan(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    # 单击
    def danji(self,loc):
        ActionChains(self.driver).click(self.find_element(*loc)).perform()
    # 键盘——回车
    def key_enter(self,loc):
        self.send_keys(loc,Keys.ENTER)
    # 浏览器退出
    def quit(self):
        self.driver.quit()
    # 浏览器刷新
    def refresh(self):
        self.driver.refresh()
    # 下拉菜单
    def Select(self,loc,info):
        Select(self.find_element(loc)).select_by_value(info)
    # 滚动
    def gundong(self,info):
        self.driver.execute_script(f'window.scrollTo(0,{info})')
    # 滚动到底部
    def gundibu(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
    # 弹窗处理
    def alert(self):
        alert=self.driver.switch_to.alert
        alert.accept()





